<?php

namespace App\Admin\Controllers;

use App\Models\Link;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class LinkController extends AdminController
{
	public function index(Content $content)
	{
		return $content
			->header(trans('admin.friend_link'))
			->description(trans('admin.list'))
			->body($this->grid()->render());
	}
	
	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid()
	{
		$grid = new Grid(new Link());
		
		$grid->id('ID')->sortable();
		$grid->name('网站名');
		$grid->url('链接');
		$grid->sort('排序')->sortable();
		
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
		});
		
		$grid->tools(function (Grid\Tools $tools) {
			$tools->batch(function (Grid\Tools\BatchActions $actions) {
				$actions->disableDelete();
			});
		});
		return $grid;
	}
	
	/**
	 * Create interface.
	 *
	 * @return Content
	 */
	public function create(Content $content)
	{
		return $content
			->header(trans('admin.friend_link'))
			->description(trans('admin.create'))
			->body($this->form());
	}
	
	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	public function form()
	{
		$user = new Link();
		
		$form = new Form($user);
		
		$form->display('id', 'ID');
		
		$form->text('name', '网站名')->rules('required');
		$form->url('url', '链接')->rules('required');
		$form->text('sort', '排序值')->rules('required|regex:/^\d+$/', [
			            'regex' => '排序值必须为数字',
		]);
		$form->display('created_at', trans('admin.created_at'));
		$form->display('updated_at', trans('admin.updated_at'));
		
		return $form;
	}
	
	/**
	 * Edit interface.
	 *
	 * @param $id
	 *
	 * @return Content
	 */
	public function edit($id, Content $content)
	{
		return $content
			->header(trans('admin.friend_link'))
			->description(trans('admin.edit'))
			->body($this->form()->edit($id));
	}
	
	/**
	 * Show interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content
			->header(trans('admin.friend_link'))
			->description(trans('admin.detail'))
			->body($this->detail($id));
	}
}
